The problem can be found at the following link: Question Link
To arrange the given array such that all negative numbers appear first followed by positive numbers, I have used the following approach:
- Create two separate vectors,
neq
andpos
, to store the negative and positive numbers, respectively. - Traverse the given array and for each element, if it is negative, push it into the
neq
vector; otherwise, push it into thepos
vector. - Calculate the size of the
neq
vector. - Iterate from index 0 to
sz-1
and assign the elements from theneq
vector to the original array. - Iterate from index
sz
ton-1
and assign the elements from thepos
vector to the original array.
- Time Complexity:
O(n)
since we traverse the given array once to separate negative and positive numbers and then assign the elements back to the original array. - Space Complexity:
O(n)
since we use two separate vectors,neq
andpos
, to store the negative and positive numbers, respectively.
class Solution {
public:
void Rearrange(int arr[], int n) {
vector<int> neq, pos;
for(int i = 0; i < n; ++i) {
if(arr[i] < 0) {
neq.push_back(arr[i]);
} else {
pos.push_back(arr[i]);
}
}
int sz = neq.size();
for(int i = 0; i < sz; ++i)
arr[i] = neq[i];
for(int i = sz; i < n; ++i)
arr[i] = pos[i - sz];
}
};
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.